
#HLA EPITOPES FREQUENCY DATABASE
#Function: Insert data into [epit_loci] table [EpitHLA] schema
#Created in: 29.01.2014
#Author: Louise Takeshita


use strict;
use EpiHash qw(epi_data);

#Process Epitope data into HashMap
my %epitopes = epi_data; # returns epitope hashmap

#Create output file
open(OUTPUT,">insert_epitalleles.sql") || die "The file insert_epitalleles.sql could not be opened.\n"; #open file and handle unexpected errors

#Processing
for my $ep ( sort { $epitopes{$a}{'pos'} <=> $epitopes{$b}{'pos'}} keys %epitopes )
	{
	my $epit_id = $epitopes{$ep}{'pos'};
	if(exists $epitopes{$ep}{'A'})
		{
		foreach my $al(sort @{$epitopes{$ep}{'A'}})
			{
			my @nomenc = split(/\*/,$al);
			my $locus = $nomenc[0];
			my $field_1 = substr($nomenc[1],0,2);
			my $field_2 = substr($nomenc[1],2);
			print OUTPUT "INSERT INTO hla_epitopes.epit_allel VALUES ($epit_id,'$al','$locus',$field_1,$field_2);\n";
			}
		}
	if(exists $epitopes{$ep}{'B'})
		{
		foreach my $al(sort @{$epitopes{$ep}{'B'}})
			{
			my @nomenc = split(/\*/,$al);
			my $locus = $nomenc[0];
			my $field_1 = substr($nomenc[1],0,2);
			my $field_2 = substr($nomenc[1],2);
			print OUTPUT "INSERT INTO hla_epitopes.epit_allel VALUES ($epit_id,'$al','$locus',$field_1,$field_2);\n";
			}
		}
	if(exists $epitopes{$ep}{'C'})
		{
		foreach my $al(sort @{$epitopes{$ep}{'C'}})
			{
			my @nomenc = split(/\*/,$al);
			my $locus = $nomenc[0];
			my $field_1 = substr($nomenc[1],0,2);
			my $field_2 = substr($nomenc[1],2);
			print OUTPUT "INSERT INTO hla_epitopes.epit_allel VALUES ($epit_id,'$al','$locus',$field_1,$field_2);\n";
			}
		}
	}
			
print "Done!";